<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/9/13
 * Time: 17:42
 */

namespace app\admin\controller;


use think\Controller;
use app\admin\model\Order as Od;
use think\Db;
use \think\Cookie;

class Thirdparty extends Controller
{
    public function index()
    {
        //实列化模型
        $order=new Od();
        $post= request()->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $whereTime='';
        $where='';
        $date=$order;

        if (isset($post['order_no']) and $post['order_no'] > 0) {
            $where['order_no'] = $post['order_no'];
        }


        if (isset($post['order_ascription']) and $post['order_ascription'] > 0) {
            $where['order_ascription'] = $post['order_ascription'];
        }else{
            $where['order_ascription']=[['=',2],['=',3],'or'];
        }
        if (isset($post['phone']) and $post['phone'] > 0) {
            $where['phone'] = $post['phone'];
        }
//        $date=$date->where($where);
//        if (isset($post['creattime']) and $post['creattime'] != '') {
//            $shi=explode('~',$post['creattime']);
//            $date= $date->whereTime('a.creattime','between',[$shi[0],$shi[1]]);
//        }

//        if (isset($post['goods_source']) and $post['goods_source'] != ''){
//
//            $date=$date->join('goods b','a.goods_id=b.goods_id')
//                ->where('b.goods_source',$post['goods_source']);
//
//
//        }

        $where['order_status']=2;

        $date= $date->where($where) ->order('updatetime desc')->paginate(20);

//        echo date('Ymd').rand('5','99999');
//
        $this->assign('order',$date);
        $this->assign('wherelist',$post);
        return $this->fetch();
    }

    //完善订单
    public function Details(){

        $details=new Od();
        $id=input('id');
        $data=  $details->where('order_id',$id)->find();

        if (!empty($data['order_subsidiary'])){
            $data['order_subsidiary']=json_decode($data['order_subsidiary'],true) ;
        }else{
            $posts=[];
            $code= db('goods')->where('goods_id',$data['goods_id'])->find();
            $pos=json_decode($data['orderperson'] ,true);
            $data1['order']=$data['order_no'];//订单流水
            $data1['time']=$data['travel_date'];
//            $currencyCode=$this->currencyCode;
            $data1['code']=$code['code'];  //产品code
            $data1['tourGradeCode']=$data['grade'];  //旅游等级 商品详情获取 tourGrades
            $data1['orderuser']=[  //下蛋人信息- -
                'firstname'=>$data['contact_xing'],  //名
                'surname'=>$data['contact_ming'],  //姓
                'Phone'=>$data['phone'], //必填
                'email'=>$data['e_mail'],
            ];
            foreach ($pos as $v){
                $datas['bandid'][$v['person_id']]= $v['person_num'];
            }
            //虚假人员

            $alluser=array_sum($datas['bandid']);
            foreach ($datas['bandid'] as $k=>$v){
                for($i=0;$i<$v;$i++){
                    if ($i==0 && $k==1){
                        $tempList=[  //领班人/领头人
                            'leadTraveller'=>true,
                            'bandId'=>$k,
                            'firstname'=>$data['contact_xing'],
                            'surname'=>$data['contact_ming'],
                            'title'=>'',//称呼
                            'cellPhoneCountryCode'=>'',//手机国家码
                            'cellPhone'=>$data['phone'],//手机
                        ];
                    }else{
                        $tempList=[  //领班人/领头人
                            'leadTraveller'=>false,
                            'bandId'=>$k,
                            'firstname'=>$data['contact_xing'].$alluser--,
                            'surname'=>$data['contact_ming'],
                            'title'=>'',//称呼
                            'cellPhoneCountryCode'=>'',//手机国家码
                            'cellPhone'=>'',//手机
                        ];
                    }

                    $data1['bandiduser'][]=$tempList;
                }

            }
            $data['order_subsidiary']=$data1;
        }






//dump($data['order_subsidiary']);


        $this->assign('dats',$data);
        return $this->fetch();
    }



    //数据更新
    public function updata(){
        $post= request()->request();
        if (!empty($post)){
            $shu=[];
            Db::startTrans();
            try{
                $order=   db('order')->where('order_id',$post['order_id'])->find();
                $dat=json_decode($order['order_subsidiary'],true);
                $dat['bandiduser']=$post['data'];

                $d= db('order')->where('order_id',$post['order_id'])->update([
                    'order_subsidiary'=>json_encode($dat),
                    'update_status'=>1
                ]);

                $shu=[
                    'code'=>1,
                    'msg'=>'修改成功',
                ];
                Db::commit(); //提交事务
            } catch (\Exception $e) {
                Db::rollback();
                $shu=[
                    'code'=>101,
                    'msg'=>'失败',
                    'error'=>$e->getMessage()
                    //                    ‘'data'=>$data
                ];
            }
            return $shu;
        }

    }



}